package com.hurst.autotap;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import java.util.Timer;
import java.util.TimerTask;

public class UtilCls {
    /**
     * 延时执行任务
     * @param runnable
     * @param delay
     * @param runonce
     */
    public static TimerTask DelayTask(final Runnable runnable, int delay, final boolean runonce){
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                runnable.run();
                if(runonce){
                    this.cancel();
                }
            }
        };
        timer.schedule(task, delay);
        return task;
    }

    public static boolean GetBluetoothPermission(Activity context){
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            //Log.i(TAG, "sdk < 28 Q");
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                String[] strings = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
                ActivityCompat.requestPermissions(context, strings, 1);
                return false;
            }
        } else {
            if (ActivityCompat.checkSelfPermission(context,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {//|| ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(context, "android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED
                String[] strings = {android.Manifest.permission.ACCESS_FINE_LOCATION};//, android.Manifest.permission.ACCESS_COARSE_LOCATION, "android.permission.ACCESS_BACKGROUND_LOCATION"
                ActivityCompat.requestPermissions(context, strings, 2);
                return false;
            }
        }
        return true;
    }
    public static void SLog(String tag,String msg){
        Log.v(tag,msg);
    }
}